//facebook app id
appid='';
channelurl='';

//function to get current userid
function getUser()
{
    FB.init({
        appId      : '1420018338231105', // App ID
        channelUrl :' http://localhost:8080/Trade/h1.html', // Channel File
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true  // parse XFBML
    });
    //check current user login status
    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            loadFriends();
        } else {
            //user is not connected.
            FB.login(function(response) {
                if (response.authResponse) {
                    loadFriends();
                } else {
                    alert('User cancelled login or did not fully authorize.');
                }
            });
        }
    });
}

//function to load friends
function loadFriends()
{
    //get array of friends
    FB.api('/me/friends', function(response) {
        console.log(response);
        var divContainer=$('.facebook-friends');
        for(i=0;i<response.data.length;i++)
            {
                $(document.createElement("img")).attr({ src: 'https://graph.facebook.com/'+response.data[i].id+'/picture', title: response.data[i].name ,onClick:'alert("You poked "+this.title);'})
                .appendTo(divContainer);       
            }
    });
}

//start from here
$(document).ready(function(){
    $('.load-button').click(function(){
        getUser();
    });
});